package cj.web.portal.controllers;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.kly.domain.GroupDep;
import cj.web.kly.domain.GroupInfor;
import cj.web.kly.domain.GroupPeople;
import cj.web.login.controllers.BaseController;
import cj.web.portal.service.InstitutionService;
import cj.web.portal.service.PeopleService;

/**
 * 机构设置
 * @author Administrator
 *
 */
@Path("/institution")
public class InstitutionController extends BaseController {

	private static Logger logger = Logger.getLogger(InstitutionController.class.getName());
	
	@Autowired
	private InstitutionService institutionService;
	
	@Autowired
	private PeopleService peopleService;
	
	/**
	 * 机构设置
	 * @return
	 * @throws Exception 
	 */
	@Get("organDepartment")
	public String organDepartment(Model model,@Param("groupId") int groupId) throws Exception {
		model = institutionService.queryGroupDeplis(model, groupId);
		
		List<Map> repHeadLinklis = returnHeadLink(groupId);
		model.add("repHeadLinklis",repHeadLinklis);
		model.add("groupMemuKey",repHeadLinklis.get(0).get("value"));
		model.add("columnsMemuCn",repHeadLinklis.get(0).get("name"));
		model.add("columnsMemuEn",repHeadLinklis.get(0).get("en"));
		
		return "/views/portal/institution/organDepartment.vm";
	}
	
	/**
	 * 机构设置部门详情Fun
	 * @param depId
	 * @return
	 * @throws ParseException
	 */
	@Post("organDepartmentFun")
	public String organDepartmentFun(@Param("groupId") int groupId) throws ParseException {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<GroupDep> groupDeplis = institutionService.queryGroupDeplis(groupId);
		jo.put("groupDeplis",groupDeplis);
		return "@"+jo.toString();
	}
	
	/**
	 * 机构设置详情
	 * @param model
	 * @param id
	 * @return
	 * @throws Exception 
	 */
	@Get("organDetail")
	public String organDetail(Model model,@Param("groupId") int groupId,@Param("depId") int depId) throws Exception {
		Map rep = institutionService.queryGroupDeplisByDepId(groupId,depId);
		GroupInfor groupInfor = (GroupInfor)rep.get("groupInfor");
		List<GroupDep> groupDeplis = (List<GroupDep>)rep.get("groupDeplis");
		model.add("groupDeplis",groupDeplis);
		model.add("groupInfor",groupInfor);
		model.add("groupId",groupInfor.getId());
		model.add("depId",depId);
		
		List<Map> repHeadLinklis = returnDetailLink(groupId);
		model.add("repHeadLinklis",repHeadLinklis);
		return "/views/portal/institution/organDetail.vm";
	}
	
	/**
	 * 机构设置详情Fun
	 * @param depId
	 * @return
	 * @throws ParseException
	 */
	@Post("organDetailFun")
	public String organDetailFun(@Param("depId") int depId) throws ParseException {
		JSONObject jo = institutionService.queryGroupDepDetail(depId);
		return "@"+jo.toString();
	}
	
	//人物详情页面-部门其他人员
	@Get("organPersonDetail")
	public String detailPage(Model model,@Param("peopleId") int peopleId) throws Exception {
		Map rep = peopleService.queryByPeopleId(peopleId);
		
		List<GroupPeople> groupPeoplelis = (List<GroupPeople>) rep.get("groupPeoplelis");
		GroupInfor groupInfor = (GroupInfor) rep.get("groupInfor");
		GroupDep groupDep = (GroupDep) rep.get("groupDep");
		
		model.add("groupPeoplelis",groupPeoplelis);
		model.add("groupInfor",groupInfor);
		model.add("groupDep",groupDep);
		model.add("peopleId",peopleId);
		List<Map> repHeadLinklis = returnDetailLink(groupInfor.getId());
		model.add("repHeadLinklis",repHeadLinklis);
		return "/views/portal/institution/organPersonDetail.vm";
	}
	
	//查询用户详情
	@Post("organPersonDetailFun")
	public String detail(@Param("peopleId") int peopleId) throws ParseException {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		GroupPeople people = peopleService.queryById(peopleId);
		jo.put("people",people);
		return "@"+jo.toString();
	}
	
	/**
	 * 
	 * @param groupId
	 * @return
	 * @throws Exception
	 */
	private List<Map> returnHeadLink(int groupId) throws Exception{
		List<Map> replis = new ArrayList<Map>();
		String str = getParentName(groupId);
		
		String[] heads = str.split("&");
		for(int i=1;i<heads.length-1;i++){
			Map rep = new HashMap();
			rep.put("name", heads[i].split("@")[0]);
			rep.put("value", heads[i].split("@")[1]);
			rep.put("en", heads[i].split("@").length>2?heads[i].split("@")[2]:"");
			replis.add(rep);
		}
		return replis;
	}
	
	/**
	 * 
	 * @param groupId
	 * @return
	 * @throws Exception
	 */
	private List<Map> returnDetailLink(int groupId) throws Exception{
		List<Map> replis = new ArrayList<Map>();
		String str = getParentName(groupId);
		
		String[] heads = str.split("&");
		for(int i=1;i<heads.length;i++){
			Map rep = new HashMap();
			rep.put("name", heads[i].split("@")[0]);
			rep.put("value", heads[i].split("@")[1]);
			rep.put("en", heads[i].split("@").length>2?heads[i].split("@")[2]:"");
			replis.add(rep);
		}
		return replis;
	}
	
	/** 
     * 递归得到当前节点的所有父节点 
     * @param configId 当前节点 
     * @return 所有父节点 
     * @throws Exception 抛出的异常 
     */  
    private String getParentName(int groupId)throws Exception{  
        //和数据库交互,得到当前节点记录  
    	GroupInfor columnsEntity = institutionService.queryGroupInforlis(groupId);
        if(columnsEntity != null){  
            String configName = columnsEntity.getName()+"@"+columnsEntity.getValue()+"@"+columnsEntity.getEn()+"&";  
            //参数patrolConfigEntity.getConfigParentId()表示当前节点的父节点ID  
            String returnConfigName = getParentName(columnsEntity.getpId());  
            return returnConfigName+configName;  
        }else{  
            return "";  
        }  
    }
}